package org.tio.sitexxx.web.server.controller.minip;

import com.alibaba.fastjson.JSON;
import org.tio.http.common.HttpRequest;
import org.tio.http.server.annotation.RequestPath;
import org.tio.sitexxx.service.model.main.Knowledge;
import org.tio.sitexxx.service.model.main.Member;
import org.tio.sitexxx.service.service.qj.KnowledgeService;
import org.tio.sitexxx.service.service.qj.MemberService;

import java.util.HashMap;
import java.util.Map;


@RequestPath(value ="/notice")
public class NoticeController {

    /**
     * 查找是否有新的公告
     * @param openid
     * @return
     */
    @RequestPath(value ="/notice")
    public Map<String, Object> notice(String openid) {
        System.out.println("notice");
        Map<String, Object> query = new HashMap<>();
        Map<String, Object> result = new HashMap<>();

        Knowledge notice = KnowledgeService.me.getNotice();

        query.put("openid", openid);
        Member Member = MemberService.me.getMember(query);

        if (notice == null) {
            result.put("ok", 1);
            result.put("msg", "没有公告");
            return result;
        }

        if (Member.getReadNotice() == notice.getId()) {
            result.put("ok", 1);
            result.put("msg", "公告已读");
            return result;
        }
        //当用户已读公告与now公告不一致时 把now公告传给用户
        result.put("ok", 0);
        result.put("notice", notice);
        return result;
    }

    /**
     * 当用户点击以读时进入这个路由
     * @param request
     * @return
     */
    @RequestPath(value ="/read")
    public Map<String, Object> read(HttpRequest request){
        System.out.println("notice read");
        Map<String,Object>body= JSON.parseObject(request.getBodyString());
        String openid = (String) body.get("openid");
        Integer noticeId = (Integer) body.get("noticeId");
        Map<String, Object> query = new HashMap<>();
        Map<String, Object> result = new HashMap<>();

        query.put("openid", openid);
        Member Member = MemberService.me.getMember(query);

        Member.setReadNotice(noticeId);
        MemberService.me.update(Member);

        result.put("ok",1);
        return result;
    }

}
